package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.dal.entitys.Stock;
import com.cskaoyan.order.dal.persistence.StockMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class ValidateHandler extends AbstractTransHandler {



    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext createOrderContext= (CreateOrderContext) context;
        if (createOrderContext.getUserId()==null|| createOrderContext.getUserName()==null
               ){
            throw new BizException(OrderRetCode.USER_DETAIL_FAIL .getCode(),
                    OrderRetCode.USER_DETAIL_FAIL .getMessage());
        }
        if (createOrderContext.getAddressId()==null||createOrderContext.getTel()==null||createOrderContext.getStreetName()==null){
            throw new BizException(OrderRetCode.ADRESS_DETAIL_FAIL .getCode(),
                    OrderRetCode.ADRESS_DETAIL_FAIL .getMessage());
        }
        if (createOrderContext.getCartProductDtoList()==null||
                createOrderContext.getOrderTotal()==null
        ){
            throw new BizException(OrderRetCode.CART_DETAIL_FAIL .getCode(),
                    OrderRetCode.CART_DETAIL_FAIL.getMessage());
        }
        if (createOrderContext.getUniqueKey()==null){
            throw new BizException(OrderRetCode.INIT_ORDER_EXCEPTION .getCode(),
                    OrderRetCode.INIT_ORDER_EXCEPTION.getMessage());
        }

        return true;
    }
}
